﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OpenTK.Graphics.OpenGL;
using System.Threading;
using OpenTK.Graphics;
using OpenTK;
using Tao.FreeGlut;
using Tao.OpenGl;
using OpenTK.Input;

namespace opentk_first_project
{
    public partial class Form1 : Form
    {
        FastLoop _fastLoop ;
        StateSystem _system;
        TextureManager _textureManager;
        Camera _camera;
        private int timeEachFrame = 16;
        public Form1()
        {
            InitializeComponent();
            _fastLoop = new FastLoop(GameLoop);
            _system = new StateSystem();
            _camera = new Camera(this.ClientSize.Width, this.ClientSize.Height);
            _textureManager = new TextureManager();    
            glControl1.Load +=new EventHandler(glControl1_Load);
            glControl1.Resize +=new EventHandler(glControl1_Resize);
            
            ClientSize = new Size(1280, 720);
        }
        protected override void OnClosing(CancelEventArgs e)
        {
            base.OnClosing(e);
        }
        /*
         *GL control loader 
         */
        bool loaded = false;
        private void glControl1_Load(object sender, EventArgs e)
        {
            _system.AddState("test_sprite", new TestSpriteClass(_textureManager,_camera));
            _system.ChangeState("test_sprite");
            loaded = true;
        }
        /*
         * GL control resize
         */
        private void glControl1_Resize(object sender, EventArgs e)
        {
            GL.Viewport(0, 0, this.ClientSize.Width, this.ClientSize.Height);
            _camera.UpdateSize(this.ClientSize.Width, this.ClientSize.Height);
        }
        private void GameLoop(double elapsedTime)
        {
            if (!loaded)
            {
                return;
            }
            // ----------------------------Begin loop-----------------------------------------
            FrameCounter.frame_shown();
            double startTime = DateTime.Now.TimeOfDay.TotalMilliseconds;
            // Update input and process
            _system.Update(elapsedTime);
            // Renderer
            _system.Render();
            GL.Finish(); // all GL command finish - end of frame            
            glControl1.SwapBuffers(); //double buffer
            //debug show
            fps.Text = FrameCounter.get_framerate().ToString(); // fps show
            
            // ------------------------------End loop------------------------------------------

            /*
             * Sleep - avoid consume CPU
             */
            double endTime = DateTime.Now.TimeOfDay.TotalMilliseconds;
            int delta = (int)((endTime - startTime));
            if (delta > timeEachFrame)
            {
                delta = timeEachFrame;
            }
            Thread.Sleep(timeEachFrame - delta);        
        }



    }
}
